<?php
class ModelConfigNews extends Model {
	public function editNews($menu_route_id, $data) {
		$this->db->query("delete from `".DB_PREFIX."message_news`  where menu_route_id='".(int)$menu_route_id."'");
		$this->db->query("delete from `".DB_PREFIX."message_news_article`  where menu_route_id='".(int)$menu_route_id."'");
		$this->db->query("insert into `".DB_PREFIX."message_news` set create_time=now(),menu_route_id='".(int)$menu_route_id."'");
		$lastId = $this->db->getLastId();
		
		if(isset($data['article_route'])){
			foreach ($data['article_route'] as $article) {
				$this->db->query("insert into `".DB_PREFIX."message_news_article` set news_id='".(int)$lastId."',menu_route_id='".(int)$menu_route_id."',title='".$article['title']."',description='".$this->db->escape($article['description'])."',pic_url='".$article['pic_url']."',url='".$article['url']."'");
			}
		}
		
	}
	
	public function getNewsArticle($news_id){
		$sql ="select * from ".DB_PREFIX."message_news_article where news_id='".(int)$news_id."'";
		return $this->db->query($sql)->rows;
	}
	
	public function getNewsArticleByUrl($menu_url){
		$sql ="select mna.* from ".DB_PREFIX."message_news_article mna left join ".DB_PREFIX."config_menu_route cmc on(cmc.menu_route_id=mna.menu_route_id) where cmc.menu_url='".$menu_url."'";
		return $this->db->query($sql)->rows;
	}
	
	public function getNews($menu_route_id) {
	    $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "message_news` WHERE menu_route_id = '" . (int)$menu_route_id . "'");
		$news = $query->row;
		if($news){
			$result = array(
				'create_time'=>$news['create_time'],
				'articles'=>$this->getNewsArticle($news['news_id']),
			);
			return $result;
		}
		return false;
	}
	
}
?>